package com.qfedu.service;

import com.qfedu.dao.StudentDao;
import com.qfedu.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired // 去容器中查找StudentDao 并设置进来
    private StudentDao studentDao;

    @Override
    public Student findStudentById(int id) {
        return studentDao.findStudentById(id);
    }

    // 开启事务
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED
    ,timeout = 2,rollbackFor=Exception.class) // 为当前方法 开启事务
    @Override
    public int deleteStudentByIds(List<Integer> ids) {

        // 当前方法执行多条sql
        // 事务 如果 一条增删改sql 失败都失败 其他成功的 回滚
        for (Integer id : ids) {
           int num =  studentDao.deleteStudentById(id);
           int i = 1/0;
           if (num==0){
               return 0;
           }
        }

        return 1;
    }
}
